const { tenantsModel } = require('./models/tenantsModel');

module.exports.getTenants = async function(query) {
    const { pageSize, currentPage, _id, agreenState, input } = query;
    if (_id) {
        await tenantsModel.updateOne({ _id }, { state: agreenState })
    }
    if (input == undefined) {
        const data = await tenantsModel.find().limit(pageSize - '0').skip((currentPage - 1) * pageSize);
        const total = await tenantsModel.countDocuments();
        return { data, total };
    } else {
        console.log(input);
        const data = await tenantsModel.find({
            IDCard: { $regex: input }
        }).limit(pageSize - '0').skip((currentPage - 1) * pageSize);
        const total = data.length;
        return { data, total };
    }

}